    package com.zju.springboot.db.redis;

    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.cache.RedisCacheConfiguration;
    import org.springframework.data.redis.cache.RedisCacheManager;
    import org.springframework.data.redis.cache.RedisCacheWriter;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.RedisSerializationContext;
    import org.springframework.data.redis.serializer.StringRedisSerializer;

    import java.time.Duration;
    import java.util.HashMap;
    import java.util.Map;

    @Data //为ttlMap提供提供getset方法
    @Configuration
    @ConfigurationProperties(prefix = "caching") //引入配置文件application-dev.yml中的caching配置
    public class RedisConfig {

        /**
         * 实现自定义序列化方法
         * @param redisConnectionFactory
         * @return
         */
        @Bean
        public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
            RedisTemplate redisTemplate = new RedisTemplate();
            redisTemplate.setConnectionFactory(redisConnectionFactory);
            Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

            jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

            //重点在这四行代码
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
            redisTemplate.setHashKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);

            redisTemplate.afterPropertiesSet();
            return redisTemplate;
        }
        /**
         * 实现自定义个性化缓存配置
         * @param redisTemplate
         * @return
         */
        //自定义redisCacheManager
        @Bean
        public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate) {
            RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory());

            RedisCacheManager redisCacheManager = new RedisCacheManager(redisCacheWriter,
                    this.buildRedisCacheConfigurationWithTTL(redisTemplate,RedisCacheConfiguration.defaultCacheConfig().getTtl().getSeconds()),  //默认的redis缓存配置
                    this.getRedisCacheConfigurationMap(redisTemplate)); //针对每一个cache做个性化缓存配置

            return  redisCacheManager;
        }
        //配置注入，key是缓存名称，value是缓存有效期
        private Map<String,Long> ttlmap;  //lombok提供getset方法

        //根据ttlmap的属性装配结果，个性化RedisCacheConfiguration
        private Map<String, RedisCacheConfiguration>  getRedisCacheConfigurationMap(RedisTemplate redisTemplate){
            Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();

            for(Map.Entry<String, Long> entry : ttlmap.entrySet()){
                String cacheName = entry.getKey();
                Long ttl = entry.getValue();
                redisCacheConfigurationMap.put(cacheName,this.buildRedisCacheConfigurationWithTTL(redisTemplate,ttl));
            }

            return redisCacheConfigurationMap;
        }

        //根据传参构建缓存配置
        private RedisCacheConfiguration buildRedisCacheConfigurationWithTTL(RedisTemplate redisTemplate,Long ttl){
            return  RedisCacheConfiguration.defaultCacheConfig()
                    .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer()))
                    .entryTtl(Duration.ofSeconds(ttl));
        }
    }
